<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ include file="/WEB-INF/decorators/taglibs.jsp"%>
<div class="w1a">
<div class="w2">資料維護 » 案次決定  </div>
	<br/> 
	<form:form modelAttribute="searchForm" id="searchForm" method="post">
	<form:hidden path="sessionNo" id="sessionNo"/>
	<h2>會期${searchForm.sessionNo}</h2>
	<a class="easyui-linkbutton" href="${appPath}/session.htm">返回</a>
	<table id="dg" class="easyui-datagrid" style="height:600px;width:1000px;" url="${appPath}/motion/chooseList.json"  
		striped="true" title="案次決定"
		data-options="
		singleSelect: true,
		toolbar: '#tb',
        method: 'post',
        onBeforeLoad: onBeforeLoad,
        onClickCell: onClickCell,
        onBeforeEdit: onBeforeEdit,
        onAfterEdit: onAfterEdit,
        onCancelEdit: onCancelEdit,
		">
		<thead><tr>
			<th field="sessionNo" width="80" formatter="sessionFormat"
			data-options="editor:{type:'combobox',
							options:{
                                url:'${appPath}/session/${searchForm.sessionNo}/findSession.json',
                                method:'get',
                                valueField:'sessionNo',
                                textField:'sessionNo',
                                required:true,
                            }}" 
			>
			會期</th>
			<th field="unitCode" width="40" >單位</th>
	        <th field="typeNo" width="40" formatter="typeFormat">案別</th>
	        <th field="provisional" width="40" formatter="provisional">臨時</th>
	        <th field="motionOrder" width="40" maxlength="2" data-options="editor:{type:'text',options:{maxLength:2}}">案次</th>
	        <th field="date2" width="100" >日期</th>
	        <th field="cause" width="500" >案由</th>
	        <th field="action" width="100" formatter="action">作業</th>  
		</tr></thead>   
	</table> 
	<div id="tb" style="height: auto">   
		<a href="javascript:void(0)" class="easyui-linkbutton" onclick="endEditing()">存檔</a>
		<a href="javascript:void(0)" class="easyui-linkbutton" onclick="endEditing()">取消</a>
	</div>
   </form:form>
</div>
<script>
$.ajaxSetup({ cache: false });
var editIndex = undefined; // 目前編輯的row index
function endEditing() {
	if (editIndex == undefined) {
		return true;
	}
	if ($("#dg").datagrid('validateRow', editIndex)) {
		$('#dg').datagrid('endEdit', editIndex);
		$('#dg').datagrid('cancelEdit', editIndex);
		editIndex = undefined;
		return true;
	} else {
		/*取消編輯狀態*/
		var ed = $("#dg").datagrid("getEditor", {index:editIndex,field:"name"});
		if($(ed.target).text('getValue')==""){
			$('#dg').datagrid('endEdit', editIndex);
			$('#dg').datagrid('cancelEdit', editIndex);
			return true;
		}
		return false;
	}
}
function onBeforeLoad(param){
	$.each($('#searchForm').serializeArray(), function(i, v) { param[v.name] = v.value;});
}
function onClickCell(index, field, value){
	if(field!='action'){
		if (endEditing()) {
			$("#dg").datagrid("selectRow", index).datagrid('beginEdit',index);
			editIndex = index;
		} else {
			$("#dg").datagrid("selectRow", editIndex);
		}
	}
}
function onBeforeEdit(index, row, changes) {
	row.editing = true;
    $("#dg").datagrid("refreshRow", index);
}
function onAfterEdit(index, row, changes) {
		row.editing = false;
	    $("#dg").datagrid("refreshRow", index);
	if (Object.keys(changes).length > 0) { 
	    update(index);
	}
}
function onCancelEdit(index, row, changes) {
	row.editing = false;
    $("#dg").datagrid("refreshRow", index);
}
function update(index) {
	var o = $("#dg").datagrid("getData").rows[index];
	$.post("${appPath}/motion/updateOrder.json", 
		{"motionSeq":o.motionSeq,
		"sessionNo":o.sessionNo,
		"typeNo":o.typeNo,
		"motionOrder":o.motionOrder},
		function(data) {
			if (!eval(data)) {
				alert("更新失敗!");
			}else{
				$('#dg').datagrid('load'); 
			}
	});
}
function getRowIndex(target){
    var tr = $(target).closest("tr.datagrid-row");
    return parseInt(tr.attr("datagrid-row-index"));
}
function del(target){
    $.messager.confirm("Confirm","確定落選?",function(r){
        if (r){
        	var index = getRowIndex(target);
        	var o = $("#dg").datagrid("getData").rows[index];
            $.post("${appPath}/motion/exclude/"+o.motionSeq+".json", 
       			function(data) {
            	console.log(data);
       				if (eval(data)) {
       					$("#dg").datagrid("deleteRow", index);
       				} else {
       					alert("落選失敗!");
       				}
       		});
        }
    }); 
}
function cancel(){
    $("#dg").datagrid("cancelEdit", editIndex);
    editIndex = undefined;
}
function provisional(val, row, ix) {
	return eval(val)?"✔":"";
}
function action(val, row, ix) {var str = "";
	str = str.concat("[<a href='#do' onclick=\"del(this);\" >落選</a>]");
	return str; 
}
function typeFormat(val, row, ix){
	return "<font color='blue'>"+val+"</font>";
}
function sessionFormat(val,row,ix){
	if(row.sessionNo==null||row.sessionNo==""){
		row.sessionNo = row.session.sessionNo;
	}
	return  row.sessionNo ; 
}

</script>